package com.yunkaweilai.android.activity.member;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 新增会员等级
 * Created by Administrator on 2018/6/27.
 */

public class AddMemberLevelActivity extends BaseActivity {

    @BindView(R.id.titlebar_iv_left)
    ImageView titlebarIvLeft;
    @BindView(R.id.titlebar_tv_left)
    TextView titlebarTvLeft;
    @BindView(R.id.titlebar_tv)
    TextView titlebarTv;
    @BindView(R.id.titlebar_iv_right_2)
    ImageView titlebarIvRight2;
    @BindView(R.id.titlebar_iv_right)
    ImageView titlebarIvRight;
    @BindView(R.id.titlebar_tv_right)
    TextView titlebarTvRight;
    @BindView(R.id.id_view_underline)
    View idViewUnderline;
    @BindView(R.id.rl_titlebar)
    RelativeLayout rlTitlebar;
    @BindView(R.id.id_edt_member_name)
    EditText idEdtMemberName;
    @BindView(R.id.id_edt_card_price)
    EditText idEdtCardPrice;
    @BindView(R.id.id_password_switch)
    Switch idPasswordSwitch;
    @BindView(R.id.id_password_switch_jici)
    Switch idPasswordSwitchJici;
    @BindView(R.id.id_switch_points)
    Switch idSwitchPoints;
    @BindView(R.id.id_edt_points)
    EditText idEdtPoints;
    @BindView(R.id.id_edt_points2)
    EditText idEdtPoints2;
    @BindView(R.id.id_swithc1)
    Switch idSwithc1;
    @BindView(R.id.id_edt_1)
    EditText idEdt1;
    @BindView(R.id.id_layout1)
    LinearLayout idLayout1;
    @BindView(R.id.id_view1)
    View idView1;
    @BindView(R.id.id_swithc2)
    Switch idSwithc2;
    @BindView(R.id.id_edt_2)
    EditText idEdt2;
    @BindView(R.id.id_layout2)
    LinearLayout idLayout2;
    @BindView(R.id.id_view2)
    View idView2;
    @BindView(R.id.id_swithc3)
    Switch idSwithc3;
    @BindView(R.id.id_layout31)
    LinearLayout idLayout31;
    @BindView(R.id.id_layout32)
    LinearLayout idLayout32;
    @BindView(R.id.id_layout33)
    LinearLayout idLayout33;
    @BindView(R.id.id_layout_point_all)
    LinearLayout idLayoutPointAll;
    @BindView(R.id.id_switch_zhekou)
    Switch idSwitchZhekou;
    @BindView(R.id.id_edt_nums)
    EditText idEdtNums;
    @BindView(R.id.id_layout_zhekou_all)
    LinearLayout idLayoutZhekouAll;
    @BindView(R.id.id_switch_chuzhi)
    Switch idSwitchChuzhi;
    @BindView(R.id.id_layout_chuzhi1)
    LinearLayout idLayoutChuzhi1;
    @BindView(R.id.id_layout_chuzhi2)
    LinearLayout idLayoutChuzhi2;
    @BindView(R.id.id_layout_chuzhi_all)
    LinearLayout idLayoutChuzhiAll;
    @BindView(R.id.id_layout3)
    LinearLayout idLayout3;
    @BindView(R.id.id_switch_chuzhi1)
    Switch idSwitchChuzhi1;
    @BindView(R.id.id_layout_chuzhi0)
    LinearLayout idLayoutChuzhi0;
    private LayoutInflater inflater;
    private int is_password = 0;//支付密码
    private int is_password_jici = 0;//扣次密码
    private int is_points = 0;//积分功能
    private int is_arrived_points = 0;//积分抵现
    private int is_birthday_points = 0;//会员生日
    private int is_recharge_points = 0;//充值积分规则
    private int is_discount = 0;//折扣功能
    private int is_stored = 0;//储值功能
    private int is_recharge_limit = 0;//储值赠送规则

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_member_level_layout);
        ButterKnife.bind(this);
        new TitleBuilder(mAc).setLeftImage(R.mipmap.ic_go_back).setLeftOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        }).setRightText("保存").setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setRight();
            }
        }).setTitleText("新增会员等级");
        inflater = LayoutInflater.from(mAc);
        switchClick();
        initClick();

    }

    private void setRight() {
        if (Utils.isEmpty(idEdtMemberName.getText().toString())){
            showToast("未输入等级名称");
            return;
        }
        if (is_arrived_points == 1){
            if (Utils.isEmpty(idEdt1.getText().toString())){
                showToast("未输入积分抵现");
                return;
            }
        }
        showProgressDialog();
        saveHttp();
    }

    private void saveHttp() {
        PostProcess postProcess = HttpRequest.postUrl(Api.ADD_MEMBER_LEVEL)
                .addParams("level_name", idEdtMemberName.getText().toString())
                .addParams("activate_amount", idEdtCardPrice.getText().toString())
                .addParams("validity_type", "permanent")
                .addParams("is_password", is_password + "")
                .addParams("is_jici_password", is_password_jici + "")
                .addParams("is_points", is_points + "")
                .addParams("open_points", idEdtPoints.getText().toString() + "")
                .addParams("consumption_points", idEdtPoints2.getText().toString() + "")
                .addParams("is_recharge_points", is_recharge_points + "")
                .addParams("is_arrived_points", is_arrived_points + "")
                .addParams("arrived_amount", idEdt1.getText().toString() + "")
                .addParams("is_birthday_points", is_birthday_points + "")
                .addParams("multiple", idEdt2.getText().toString() + "")
                .addParams("is_discount", is_discount + "")
                .addParams("discount", idEdtNums.getText().toString() + "")
                .addParams("is_stored", is_stored + "")
                .addParams("recharge_rule_type", "limit")
                .addParams("is_recharge_limit",is_recharge_limit+"");
        for (int i = 0; i < idLayout32.getChildCount(); i++) {
            View views1 = idLayout32.getChildAt(i);
            EditText edt1 = (EditText) views1.findViewById(R.id.id_edt_1);
            EditText edt2 = (EditText) views1.findViewById(R.id.id_edt_2);
            if (Utils.isEmpty(edt1.getText().toString()) || Utils.isEmpty(edt2.getText().toString())){

            }else{
                postProcess.addParams("rechargerule[price][" + i + "]",edt1.getText().toString());
                postProcess.addParams("rechargerule[points][" + i + "]",edt2.getText().toString());
            }
        }
        for (int i = 0; i < idLayoutChuzhi1.getChildCount(); i++) {
            View views1 = idLayoutChuzhi1.getChildAt(i);
            EditText edt1 = (EditText) views1.findViewById(R.id.id_edt_1);
            EditText edt2 = (EditText) views1.findViewById(R.id.id_edt_2);
            if (Utils.isEmpty(edt1.getText().toString()) || Utils.isEmpty(edt2.getText().toString())){

            }else{
                postProcess.addParams("rechargelimit[price][" + i + "]",edt1.getText().toString());
                postProcess.addParams("rechargelimit[sending_price][" + i + "]",edt2.getText().toString());
            }
        }
        postProcess.execute(new PostProcess.StringCallBack() {
            @Override
            public void onResponse(String json) {
                dismissProgressDialog();
                boolean checkCode = Utils.checkCode(mAc,json);
                if (checkCode){
                    setResult(RESULT_OK);
                    finish();
                }
            }

            @Override
            public void onError(Exception e) {
                dismissProgressDialog();

            }
        });


    }

    //开关状态
    private void switchClick() {
        //是否开启密码
        idPasswordSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_password = isChecked ? 1 : 0;
            }
        });
        //是否开启扣次密码
        idPasswordSwitchJici.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_password_jici = isChecked ? 1 : 0;
            }
        });
        //积分功能
        idSwitchPoints.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_points = isChecked ? 1 : 0;
                if (isChecked) {
                    idLayoutPointAll.setVisibility(View.VISIBLE);
                } else {
                    idLayoutPointAll.setVisibility(View.GONE);
                }
            }
        });
        //积分抵现
        idSwithc1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_arrived_points = isChecked ? 1 : 0;
                if (isChecked) {
                    idLayout1.setVisibility(View.VISIBLE);
                } else {
                    idLayout1.setVisibility(View.GONE);
                }
            }
        });
        //会员生日
        idSwithc2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_birthday_points = isChecked ? 1 : 0;
                if (isChecked) {
                    idLayout2.setVisibility(View.VISIBLE);
                } else {
                    idLayout2.setVisibility(View.GONE);
                }
            }
        });
        //充值积分规则
        idSwithc3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_recharge_points = isChecked ? 1 : 0;
                if (isChecked) {
                    idLayout3.setVisibility(View.VISIBLE);
                } else {
                    idLayout3.setVisibility(View.GONE);
                }
            }
        });
        //折扣功能
        idSwitchZhekou.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_discount = isChecked ? 1 : 0;
                if (isChecked) {
                    idLayoutZhekouAll.setVisibility(View.VISIBLE);
                } else {
                    idLayoutZhekouAll.setVisibility(View.GONE);
                }
            }
        });
        //储值功能
        idSwitchChuzhi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_stored = isChecked ? 1 : 0;
                if (isChecked) {
                    idLayoutChuzhiAll.setVisibility(View.VISIBLE);
                } else {
                    idLayoutChuzhiAll.setVisibility(View.GONE);
                }
            }
        });
        //储值赠送规则
        idSwitchChuzhi1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_recharge_limit = isChecked ? 1 : 0;
                if (isChecked) {
                    idLayoutChuzhi0.setVisibility(View.VISIBLE);
                } else {
                    idLayoutChuzhi0.setVisibility(View.GONE);
                }
            }
        });
    }

    private void initClick() {
        idLayout33.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addView1();
            }
        });

        idLayoutChuzhi2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addView2();
            }
        });
    }

    private void addView1() {
        View views = inflater.inflate(R.layout.add_view_guize1, null);
        idLayout32.addView(views);
        ImageView idImgClose = (ImageView) views.findViewById(R.id.id_img_close);
        idImgClose.setTag(views);
        idImgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View tagView = (View) v.getTag();
                removeViews1(tagView);
            }
        });
    }
    private void addView2() {
        View views = inflater.inflate(R.layout.add_view_guize1, null);
        idLayoutChuzhi1.addView(views);
        ImageView idImgClose = (ImageView) views.findViewById(R.id.id_img_close);
        idImgClose.setTag(views);
        idImgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View tagView = (View) v.getTag();
                removeViews2(tagView);
            }
        });
    }

    private void removeViews1(View views) {
        idLayout32.removeView(views);
    }
    private void removeViews2(View views) {
        idLayoutChuzhi1.removeView(views);
    }
}
